package com.osmino.contacts.groups.add;

import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.osmino.contacts.R;
import com.osmino.contacts.entity.Group;
import com.osmino.contacts.icon.ChooseIconActivity;
import com.osmino.contacts.list.ContactViewRingtoneItem;
import com.osmino.contacts.provider.ContactsContractQueries;
import com.osmino.core.AbsListActivity;

public final class AddGroupActivity extends AbsListActivity implements TextWatcher, OnClickListener {

	private EditText mGroupEditText;
	private Button mPositiveButton;
	private AccountAdapter mAdapter;
	private ContactViewRingtoneItem mRingtoneItem;
	private IconItem mIconItem;
	private Group mGroup;
	private static final int RINGTONE_REQUEST_CODE = 0;
	private static final int ICON_REQUEST_CODE = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mGroup = Group.createDraftGroup(this);
		setContentView(R.layout.group_add_layout);
		mGroupEditText = (EditText) findViewById(R.id.group_title_edittext);
		mAdapter = new AccountAdapter(this);
		setListAdapter(mAdapter);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		mPositiveButton = (Button) findViewById(R.id.positive_button);
		Button negativeButton = (Button) findViewById(R.id.negative_button);
		mGroupEditText.addTextChangedListener(this);
		mPositiveButton.setOnClickListener(this);
		negativeButton.setOnClickListener(this);
		addRingtoneAndIconItems();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mAdapter.setListAccounts(ContactsContractQueries.listAvailableAccounts(this));
		if (getListView().getCheckedItemPosition() <0) {
			getListView().setItemChecked(0, true);
		}
		checkEnableStateOfPositiveButton();
	}

	@Override
	public void afterTextChanged(Editable s) {
		checkEnableStateOfPositiveButton();
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {//
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {//
	}
	
	@Override
	public void onClick(View view) {
		int viewId = view.getId();
		switch (viewId) {
			case R.id.ringtone_item:
				showRingtonePicker();
				break;
			case R.id.icon_item:
				showIconPicker();
				break;
			case R.id.positive_button:
				createGroup();
				finish();
				break;
			case R.id.negative_button:
				finish();
				break;
		}
	}


	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case RINGTONE_REQUEST_CODE:
				onRingtoneRequestCallback(resultCode, data);
				break;
			case ICON_REQUEST_CODE:
				onIconRequestCallback(resultCode, data);
				break;
		}
	}

	private void onRingtoneRequestCallback(int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Uri ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			mGroup.setRingtone(this, ringtone);
		}		
	}
	
	private void onIconRequestCallback(int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			int pickedIcon = data.getIntExtra(ChooseIconActivity.EXTRA_PICKED_ICON, Group.ICON_DEFAULT);
			mGroup.setIconResource(this, pickedIcon);
		}		
	}

	private void addRingtoneAndIconItems() {
		ViewGroup footerViewGroup = (ViewGroup) findViewById(R.id.footer_layout);
		mRingtoneItem = new ContactViewRingtoneItem(this, null);
		mRingtoneItem.setRingtone(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
		mRingtoneItem.setId(R.id.ringtone_item);
		mRingtoneItem.setOnClickListener(this);
		footerViewGroup.addView(mRingtoneItem);
		mIconItem = new IconItem(this);
		mIconItem.setIcon(null);
		mIconItem.setId(R.id.icon_item);
		mIconItem.setOnClickListener(this);
		footerViewGroup.addView(mIconItem);
	}

	private void showIconPicker() {
		Intent activityCall = new Intent(this, ChooseIconActivity.class);
		activityCall.putExtra(ChooseIconActivity.EXTRA_EXISTING_ICON, mGroup.getIconResource());
		startActivityForResult(activityCall, ICON_REQUEST_CODE);
	}
	
	private void showRingtonePicker() {
		Intent activityCall = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
		activityCall.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, mRingtoneItem.getRingtoneUri());
		activityCall.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
		activityCall.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
		activityCall.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
		startActivityForResult(activityCall, RINGTONE_REQUEST_CODE);
	}

	private void createGroup() {
		mGroup.title = mGroupEditText.getText().toString();
		mGroup.account = mAdapter.getItem(getListView().getCheckedItemPosition());
		ContactsContractQueries.addGroup(this, mGroup);
	}
	
	private void checkEnableStateOfPositiveButton() {
		int titleLength = mGroupEditText.getText().length();
		int accountsQuantity = mAdapter.getCount();
		boolean enabled = (titleLength > 0) && (accountsQuantity > 0);
		mPositiveButton.setEnabled(enabled);
	}
}
